<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-06 09:59
 *
 * 项目：upload  -  $  - sql.php
 *
 * 作者：liwei 
 */

!defined('INLEV') && exit('Access Denied LEV');

$charset = Lev::$app['db']['charset'];

$tableOptionsInnoDB = ' CHARACTER SET '.$charset.' ENGINE=InnoDB';
$tableOptionsMyISAM = ' CHARACTER SET '.$charset.' ENGINE=MyISAM';

$tableNames = [
    '{{%levfm_datas}}',
    '{{%levfm_datas_child}}',//子表中不能存在父表中不存在的字段
    '{{%levfm_forms}}',
    '{{%levfm_fields}}',
];

$sql = <<<EOF

DROP TABLE IF EXISTS `{{%levfm_datas_child}}`;
CREATE TABLE IF NOT EXISTS `{{%levfm_datas_child}}` (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
  `uid` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '用户UID',
  `formid` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '关联表单ID',
  `srhkey` varchar(220) NOT NULL DEFAULT '' COMMENT '搜索关键字',
  `srhtype` varchar(220) NOT NULL DEFAULT '' COMMENT '分类搜索项',
  `uptime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `uptime` (`uptime`, `formid`),
  KEY `srhkey` (`srhkey`, `formid`),
  KEY `srhtype` (`srhtype`, `formid`, `uid`)
) $tableOptionsMyISAM;

DROP TABLE IF EXISTS `{{%levfm_datas}}`;
CREATE TABLE IF NOT EXISTS `{{%levfm_datas}}` (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
  `uid` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '用户UID',
  `formid` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '关联表单ID',
  `srhkey` varchar(220) NOT NULL DEFAULT '' COMMENT '搜索关键字',
  `srhtype` varchar(220) NOT NULL DEFAULT '' COMMENT '分类搜索项',
  `uniques` varchar(220) NOT NULL DEFAULT '' COMMENT '唯一字段',
  `datas` mediumtext NOT NULL COMMENT '表单数据',
  `settings` mediumtext NOT NULL COMMENT '通用设置',
  `status` tinyint(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '状态',
  `uptime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '更新时间',
  `addtime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '添加时间',
  PRIMARY KEY (`id`),
  KEY `uptime` (`uptime`, `formid`),
  KEY `srhkey` (`srhkey`, `formid`, `uid`),
  KEY `uniques` (`uniques`, `formid`)
) $tableOptionsInnoDB;

DROP TABLE IF EXISTS `{{%levfm_forms}}`;
CREATE TABLE IF NOT EXISTS `{{%levfm_forms}}` (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
  `imformid` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '导入表单ID',
  `name` varchar(64) NOT NULL DEFAULT '' COMMENT '表单名称',
  `tablename` varchar(220) NOT NULL DEFAULT '' COMMENT '数据表名',
  `descs` varchar(255) NOT NULL DEFAULT '' COMMENT '简短描述',
  `totalnum` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '数据总量',
  `totalitem` mediumtext NOT NULL COMMENT '字段统计',
  `settings` mediumtext NOT NULL COMMENT '通用设置',
  `displayorder` smallint(6) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序',
  `status` tinyint(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '状态',
  `uptime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '更新时间',
  `addtime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '添加时间',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) $tableOptionsInnoDB;

DROP TABLE IF EXISTS `{{%levfm_fields}}`;
CREATE TABLE IF NOT EXISTS `{{%levfm_fields}}` (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
  `formid` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT '关联表单ID',
  `classify` varchar(32) NOT NULL DEFAULT '' COMMENT '字段分类',
  `title` varchar(220) NOT NULL DEFAULT '' COMMENT '标题',
  `placeholder` text NOT NULL COMMENT '字段提示',
  `inputname` varchar(255) NOT NULL DEFAULT '' COMMENT '字段名',
  `inputtype` varchar(255) NOT NULL DEFAULT '' COMMENT '字段类型',
  `inputvalue` mediumtext NOT NULL COMMENT '字段默认值',
  `settings` mediumtext NOT NULL COMMENT '通用设置',
  `rules` mediumtext NOT NULL COMMENT '字段校验规则',
  `displayorder` smallint(6) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序',
  `status` tinyint(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '状态',
  `uptime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '更新时间',
  `addtime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '添加时间',
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `formid` (`formid`)
) $tableOptionsMyISAM;

EOF;

return [
    0 => $sql,
    1 => $tableNames
];
